package cn.jluzh.blog.modules.tag.controller;

import cn.jluzh.blog.commons.result.Result;
import cn.jluzh.blog.commons.result.ResultFactory;
import cn.jluzh.blog.modules.tag.entity.SysTag;
import cn.jluzh.blog.modules.tag.service.impl.TagServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "标签相关接口")
@RequestMapping("tag")
public class TagController {

    public static final int HOT_ARTICLE_TAG_NUM = 4;
    @Autowired
    TagServiceImpl tagService;

    @GetMapping("getAllTag")
    @ApiOperation("查询所有的标签")
    public Result getAllTag(){
        List<SysTag> allCategory = tagService.getAllTag();
        return ResultFactory.buildSuccessResult(allCategory);
    }

    @GetMapping("getAllTagByName")
    @ApiOperation("查询所有的标签")
    public Result getAllTagPage(@RequestParam(name="pageNo",defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize",defaultValue="10") Integer pageSize,
                                     @RequestParam(name="query", required=false) String query){
        PageHelper.startPage(pageNo, pageSize);
        List<SysTag> allTag;
        if(query!=null){
            allTag=tagService.getAllTagByName(query);
        }else {
            allTag = tagService.getAllTag();
        }
        PageInfo<SysTag> pageInfo = new PageInfo<>(allTag);
        return ResultFactory.buildSuccessResult(pageInfo);
    }

    @GetMapping("getTagById")
    @ApiOperation("获取单个标签")
    public Result getTagById(@RequestParam(name="id",required=true) Integer id){
        SysTag tagById=tagService.getTagById(id);
        return ResultFactory.buildSuccessResult(tagById);
    }

    @GetMapping("getTagByArtId")
    public Result getTagById(@RequestParam(name="id",required=true) long id){
        List<SysTag> tagByArtId = tagService.getTagByArtId(id);
        return ResultFactory.buildSuccessResult(tagByArtId);
    }

    @PostMapping("updateTag")
    @ApiOperation("修改标签信息")
    public Result updateTag(@RequestBody SysTag sysTag){
        boolean b = tagService.updateById(sysTag);
        return ResultFactory.buildSuccessResult(b);
    }

    @PostMapping("addTag")
    @ApiOperation("新增标签")
    public Result addTag (@RequestBody SysTag sysTag){
        boolean b = tagService.saveOrUpdate(sysTag);
        return ResultFactory.buildSuccessResult(b);
    }

    @DeleteMapping("deleteTag")
    @ApiOperation("删除标签")
    public Result deleteTag(@RequestParam(name = "id") Integer id){
        boolean b = tagService.removeById(id);
        return ResultFactory.buildSuccessResult(b);
    }

    /**
     * 热门
     */
    @GetMapping("/hot")
    public Result hot() {
        List<Integer> tagIds = tagService.queryHotTagIds(HOT_ARTICLE_TAG_NUM);
       List<SysTag> sysTags = new ArrayList<>();
        for (Integer tagId : tagIds) {
            SysTag tag = tagService.getById(tagId);
            sysTags.add(tag);
        }
        return ResultFactory.buildSuccessResult(sysTags);
    }

    @GetMapping("/tagTotal")
    @ApiOperation("获取标签总量")
    public Result tagTotal() {
        int count = tagService.count();
        return ResultFactory.buildSuccessResult(count);
    }
}
